package com.such.kit.mock.data.bean;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import com.such.kit.calendar.DateCalculate;
import com.such.kit.datacast.CastCommons;
import com.such.kit.placeholder.PlaceholderCommons;
import com.such.kit.validate.ValidateCommons;

public class MockDataConfig {

	private String field;
	private String expression;
	private MockDataRule[] rules;
	
	/**
	 * <p>构造函数</p>
	 * <p>描述：唯一构造函数</p>
	 * @param field 字段
	 * @param expression 表达式，如果在表达式中使用默认的 ruleTypeKey（），则被转化为对应的默认规则
	 * @param rules 规则
	 */
	public MockDataConfig(String field, String expression, MockDataRule... rules) {
		this.field = field;
		this.expression = expression;
		initRules(expression, rules);
	}

	public String getField() {
		return this.field;
	}

	public String getExpression() {
		return this.expression;
	}

	public MockDataRule[] getRules() {
		return this.rules;
	}

	private void initRules(String expression, MockDataRule... rules) {
		List<MockDataRule> ruleList = new ArrayList<MockDataRule>();
		List<String> innerKeys = PlaceholderCommons.getInnerKeys(expression);
		if (ValidateCommons.isNotEmpty(innerKeys)) {
			for (String innerKey : innerKeys) {
				if (MockDataRule.RULE_TYPE_SEQUENCE_KEY.equals(innerKey)) {
					ruleList.add(MockDataRule.sequence());
				} else if (MockDataRule.RULE_TYPE_NUMBER_KEY.equals(innerKey)) {
					ruleList.add(MockDataRule.number());
				} else if (MockDataRule.RULE_TYPE_RANDOM_KEY.equals(innerKey)) {
					ruleList.add(MockDataRule.random());
				}
			}
		}
		if (ValidateCommons.isNotEmpty(rules)) {
			for (MockDataRule rule : rules) {
				ruleList.add(rule);
			}
		}
		if (ValidateCommons.isNotEmpty(ruleList)) {
			this.rules = ruleList.toArray(new MockDataRule[0]);
		}
	}

	/**
	 * <p>方法名：defaultScope</p>
	 * <p>描述：在 valueScope 范围内取值 1 次</p>
	 * @param field
	 * @param valueScopes 取值范围
	 * @return MockDataConfig
	 */
	public static MockDataConfig defaultScope(String field, String... valueScopes) {
		return new MockDataConfig(field, MockDataRule.RULE_TYPE_SCOPE, MockDataRule.scope(valueScopes));
	}

	/**
	 * <p>方法名：floatNumber</p>
	 * <p>描述：生成浮点型数字，整数位最大长度为 integerLen，小数位最大长度为 decimalLen 且保留其小数位数</p>
	 * @param field
	 * @param integerLen 整数位最大长度
	 * @param decimalLen 小数位长度
	 * @return MockDataConfig
	 */
	public static MockDataConfig floatNumber(String field, int integerLen, int decimalLen) {
		MockDataRule i = new MockDataRule("integer", MockDataRule.RULE_TYPE_NUMBER_KEY, integerLen, false, MockDataRule.PAD_MODE_LEFT);
		MockDataRule d = new MockDataRule("decimal", MockDataRule.RULE_TYPE_NUMBER_KEY, decimalLen, true, MockDataRule.PAD_MODE_RIGHT);

		return new MockDataConfig(field, "#[integer].#[decimal]", i, d);
	}

	/**
	 * <p>方法名：dateScope</p>
	 * <p>描述：日期范围，从 start 开始的 quantity 个日期范围内取值 1 次，日期格式为 yyyy-MM-dd</p>
	 * @param field
	 * @param start 开始日期（包含）
	 * @param quantity 数量
	 * @return MockDataConfig
	 * @throws ParseException 
	 */
	public static MockDataConfig dateScope(String field, String start, int quantity) 
			throws ParseException {
		String[] valueScope = new String[quantity];
		for (int i = 0; i < quantity; i++) {
			String date = "";
			if (i == 0) {
				date = start;
			} else {
				date = DateCalculate.add(start, CastCommons.DATE, i, DateCalculate.UNIT_DAY);
			}
			valueScope[i] = date;
		}
		return new MockDataConfig(field, MockDataRule.RULE_TYPE_SCOPE, MockDataRule.scope(valueScope));
	}

}
